package com.coin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coin.domain.WebConfig;
import com.coin.model.R;
import com.coin.service.WebConfigService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @Auther: 李 力
 * @Date: 2025/6/3
 * @Description: com.coin.controller
 * @version: 1.0
 */
@Api(tags = "webConfig的控制器")
@RestController
@RequestMapping("/webConfigs")
public class WebConfigController {

    @Autowired
    private WebConfigService webConfigService;

    /**
     * @Description: 分页查询
     * @Author: 李 力
     * @Date: 2025/6/3
     * @Param: page name type
     * @return: R
     */
    @GetMapping
    @ApiOperation(value = "分页查询我们的webConfig")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "webConfig的名称"),
            @ApiImplicitParam(name = "type", value = "webConfig的类型"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页的大小"),
    })
    @PreAuthorize("hasAuthority('web_config_query')")
    public R<Page<WebConfig>> findByPage(@ApiIgnore Page<WebConfig> page, String name, String type) {
        Page<WebConfig> webConfigPage = webConfigService.findByPage(page, name, type);
        return R.ok(webConfigPage);
    }

    /**
     * @Description: 新增配置
     * @Author: 李 力
     * @Date: 2025/6/3
     * @Param: webConfig
     * @return: R
     */
    @PostMapping
    @ApiOperation(value = "新增一个WebConfig")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webConfig", value = "webConfig 的json数据")
    })
    @PreAuthorize("hasAuthority('web_config_create')")
    public R add(@RequestBody @Validated WebConfig webConfig) {
        boolean result = webConfigService.saveWebConfig(webConfig);
        return result ? R.ok() : R.fail("新增失败");
    }

    /**
     * @Description: 修改配置
     * @Author: 李 力
     * @Date: 2025/6/3
     * @Param: webConfig
     * @return: R
     */
    @PutMapping
    @ApiOperation(value = "修改一个WebConfig")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webConfig", value = "webConfig 的json数据")
    })
    @PreAuthorize("hasAuthority('web_config_update')")
    public R update(@RequestBody @Validated WebConfig webConfig) {
        boolean result = webConfigService.updateWebConfig(webConfig);
        return result ? R.ok() : R.fail("修改失败");
    }

    /**
     * @Description: 删除配置
     * @Author: 李 力
     * @Date: 2025/6/3
     * @Param: ids
     * @return: R
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除WebConfig")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "要删除的数据的ids")
    })
    @PreAuthorize("hasAuthority('web_config_delete')")
    public R update(@RequestBody String[] ids) {
        boolean result = webConfigService.removeWebConfig(ids);
        return result ? R.ok() : R.fail("删除失败");
    }

    /**
     * @Description: 获取PC端的banner图
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @GetMapping("/banners")
    public R<List<WebConfig>> banners() {
        List<WebConfig> configs = webConfigService.banners();
        return R.ok(configs);
    }
}
